package com.hejia.alauda.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.hejia.alauda.entity.Sentence;
import com.hejia.alauda.entity.example.SentenceExample;
import com.hejia.alauda.persistence.mybatis.SentenceMapper;
import com.hejia.alauda.utils.Pager;
//import com.umpay.api.util.StringUtil;

/**
 * 
 * @author jatin
 *
 */
@Service
public class SentenceServiceImpl implements SentenceService {

	@Resource
	private SentenceMapper sentenceMapper;

	@Override
	public Pager<Sentence> findSentences(Pager<Sentence> pager, String content) {

		SentenceExample example = new SentenceExample();
		if (StringUtils.isNotEmpty(content)) {
			example.appendCriterion("content like", "%" + content + "%");
		}
		example.setOrderByClause("id desc");
		pager.setList(this.sentenceMapper.selectByExample(example, pager));
		pager.setTotalCount(this.sentenceMapper.countByExample(example));

		return pager;
	}

	@Override
	public Sentence addSentence(Sentence sentence) {
		Calendar cd = Calendar.getInstance();
		cd.add(Calendar.DATE, 1);
		if (sentence.getStartTime() == null) {
			sentence.setStartTime(cd.getTime());
		}
		if (sentence.getEndTime() == null) {
			sentence.setEndTime(cd.getTime());
		}

		sentence.setCreateDate(new Date());
		sentence.setModifyDate(new Date());
		this.sentenceMapper.insert(sentence);
		return sentence;
	}

	@Override
	public void deleteSentence(Long id) {
		this.sentenceMapper.deleteByPrimaryKey(sentenceMapper
				.selectByPrimaryKey(id));
	}

	@Override
	public Sentence findSentenceById(Long id) {
		return this.sentenceMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateSentence(Sentence sentence) {
		Calendar cd = Calendar.getInstance();
		cd.add(Calendar.DATE, 1);
		if (sentence.getStartTime() == null) {
			sentence.setStartTime(cd.getTime());
		}
		if (sentence.getEndTime() == null) {
			sentence.setEndTime(cd.getTime());
		}
		sentence.setModifyDate(new Date());
		this.sentenceMapper.updateByPrimaryKey(sentence);
	}

}
